// pages/home/home.ts
import { fetchBannerListAPI, fetchCategoryAPI, fetchPropagateAPI, fetchLikeListAPI, fetchHotListAPI } from "../../api/homeAPI"
import { Data } from "../../type/homeTypes"
Page({

  /**
   * 页面的初始数据
   */
  data: <Data>{
    bannerList: [],
    categoryList: [],
    propagateList: [],
    likeList: [],
    hotList: [],
    loading: true,
  },

  /**
   *@description 获取轮播图的回调函数 
   */
  async fetchBannerList(): Promise<void> {
    try {
      const { data: { data } } = await fetchBannerListAPI()
      this.setData({ bannerList: data })
    } catch (error) {
      wx.showToast({
        icon: "error",
        title: error.message
      })
    }
  },

  /**
 *@description  获取分类的回调函数 
 */
  async fetchCategoryList(): Promise<void> {
    try {
      const { data: { data } } = await fetchCategoryAPI()
      this.setData({ categoryList: data })
    } catch (error) {
      wx.showToast({
        icon: "error",
        title: error.message
      })
    }
  },

  /**
   * @description 获取活动渲染的回调函数
   */
  async fetchPropagate(): Promise<void> {
    try {
      const { data: { data } } = await fetchPropagateAPI()
      this.setData({ propagateList: data })
    } catch (error) {
      wx.showToast({
        icon: "error",
        title: error.message
      })
    }
  },

  /**
  * @description 获取猜你喜欢的回调函数
  */
  async fetchLikeList(): Promise<void> {
    try {
      const { data: { data } } = await fetchLikeListAPI()
      this.setData({ likeList: data })
    } catch (error) {
      wx.showToast({
        icon: "error",
        title: error.message
      })
    }
  },
  /**
 * @description  获取人气推荐的回调函数
 */
  async fetchHotList(): Promise<void> {
    try {
      const { data: { data } } = await fetchHotListAPI()
      this.setData({ hotList: data })
    } catch (error) {
      wx.showToast({
        icon: "error",
        title: error.message
      })
    }
  },
  /**
  * 生命周期函数--监听页面加载
  */
  async onLoad() {
    try {
      this.setData({ loading: true })
      await Promise.all([this.fetchBannerList(), this.fetchCategoryList(), this.fetchPropagate(), this.fetchLikeList(), this.fetchHotList()])
      this.setData({ loading: false })
    } catch (error) {
      wx.showToast({
        icon: "error",
        title: error.message
      })
    }
  },
  /**
   * 
   * @description 转发
   */
  onShareAppMessage() {
    return {
      title: "",
      path: "",
      imageUrl: ""
    }
  },
  /**
 * 
 * @description 分享朋友圈
 */
  onShareTimeline() {

  }
})